from selenium import webdriver
from time import sleep
from PIL import Image
# from .api import main
from selenium.common.exceptions import NoSuchElementException
from .login import login_test
from .verification import main
import re
import os
from selenium.webdriver.chrome.options import Options


def open_browser(browser_name, url):
    driver = None
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')
    chrome_options.add_argument('--window-size=1920,1080')
    if browser_name == "chrome":
        driver = webdriver.Chrome(chrome_options=chrome_options)
    if browser_name == "ff":
        driver = webdriver.Firefox()
    if browser_name == "ie":
        driver = webdriver.Ie()
    driver.maximize_window()
    driver.implicitly_wait(10)
    driver.get(url)
    return driver


class TestKeys(object):

    def __init__(self, browser_name, url, username, password, new_password):
        self.driver = open_browser(browser_name, url)
        self.username = username
        self.password = password
        self.new_password = new_password

    def webui1(self, list_dicts):
        driver = self.driver
        # 不安全
        # driver.find_element_by_id("details-button").click()
        # sleep(2)
        # driver.find_element_by_id("proceed-link").click()
        # sleep(2)
        returns_list = []
        for dicts in list_dicts:
            # 忘记密码
            driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div[1]/form/div[5]/div/a').click()
            sleep(1)
            if dicts["email_name"] is not None:
                # 输入用户名
                driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div[2]/form/div[1]/div/div/input').send_keys(dicts["email_name"])
                sleep(1)
            if dicts["email_addr"] is not None:
                # 输入邮箱地址
                driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div[2]/form/div[2]/div/div/input').send_keys(dicts["email_addr"])
                sleep(1)
            # 找回密码
            driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div[2]/form/div[3]/div/div/div[1]/button').click()
            sleep(1)
            if dicts["email_name"] is None:
                error_text = driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div[2]/form/div[1]/div/div[2]').text
                print(error_text)
                returns_list.append(error_text)
                driver.refresh()
                continue
            if dicts["email_addr"] is None:
                error_text = driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div[2]/form/div[2]/div/div[2]').text
                print(error_text)
                returns_list.append(error_text)
                driver.refresh()
                continue
            regular = re.compile(r'[0-9a-zA-Z.]+@[0-9a-zA-Z-.]+?com')
            if re.findall(regular, dicts["email_addr"]):
                print("正确格式邮箱")
                pass
            else:
                error_text = driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div[2]/form/div[2]/div/div[2]').text
                print(error_text)
                returns_list.append(error_text)
                driver.refresh()
                continue
            # 获取弹窗文字
            email_failed_alert = driver.find_element_by_xpath('/html/body/div[2]/p').text
            print(email_failed_alert)
            sleep(2)
            returns_list.append(email_failed_alert)
            driver.refresh()
            continue
        print(returns_list)
        return returns_list

    def webui2(self, list_dicts):
        driver = self.driver
        # 不安全
        # driver.find_element_by_id("details-button").click()
        # sleep(2)
        # driver.find_element_by_id("proceed-link").click()
        # sleep(2)
        returns_list = []
        for dicts in list_dicts:
            print(dicts)
            print(dicts["username"])
            print(dicts["password"])
            # 输入用户名
            if dicts["username"] is not None:
                driver.find_element_by_css_selector("input[type='text']").send_keys(dicts["username"])
                sleep(1)
            # 输入密码
            if dicts["password"] is not None:
                driver.find_element_by_css_selector("input[type='password']").send_keys(dicts["password"])
                sleep(1)
            # # 验证码
            # driver.save_screenshot(png_path)
            # code_ele = driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div[1]/form/div[3]/div/span/img')
            # # print("验证码的坐标为：", code_ele.location)
            # # print("验证码的大小为：", code_ele.size)
            # left = code_ele.location["x"]
            # top = code_ele.location["y"]
            # right = code_ele.size["width"] + left
            # down = code_ele.size["height"] + top
            # image = Image.open(png_path)
            # code_image = image.crop((left, top, right, down))
            # code_image.save(png_path1)
            # result = main()
            # print(result)
            # a = main()
            # b = a[0]["words"]
            # b = b.replace(" ", "")
            # print(b)
            # print(type(b))
            # 输入验证码
            driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div[1]/form/div[3]/div/div[1]/input').send_keys("8888")
            sleep(1)
            # 登录
            driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div[1]/form/div[4]/div/button/span').click()
            sleep(2)
            print(dicts["username"], dicts["password"])
            if dicts["username"] is None:
                error_text = driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div[1]/form/div[1]/div/div[2]').text
                print(error_text)
                returns_list.append(error_text)
                driver.refresh()
                continue
            if dicts["password"] is None:
                error_text = driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div[1]/form/div[2]/div/div[2]').text
                print(error_text)
                returns_list.append(error_text)
                driver.refresh()
                continue
            try:
                alert_text = driver.find_element_by_xpath("/html/body/div[2]/p").text
                print(alert_text)
                returns_list.append(alert_text)
                driver.refresh()
                continue
            except Exception as a:
                returns_list.append("登录成功")

        print(returns_list)
        return returns_list

    def webui3(self, username, password):
        # 登录
        driver = login_test(username, self.driver, password)
        # 用户信息
        driver.find_element_by_xpath('//*[@id="app"]/section/header/div/aside/ul/li[1]/div').click()
        sleep(1)
        driver.find_element_by_xpath("/html/body/ul/li[1]").click()
        sleep(2)
        # 关闭
        driver.find_element_by_xpath('/html/body/div[5]/div/div[2]/div/button').click()
        sleep(2)
        return "执行成功"

    def webui4(self, list_dicts):
        # 登录
        returns_list = []
        driver = login_test(self.username, self.driver, self.password)
        for dicts in list_dicts:
            # 修改密码
            driver.refresh()
            sleep(4)
            driver.find_element_by_xpath('//*[@id="app"]/section/header/div/aside/ul/li[1]/div').click()
            sleep(2)
            driver.find_element_by_xpath("/html/body/ul/li[2]").click()
            sleep(1)
            if dicts["original_password"] is not None:
                # 输入原密码
                driver.find_element_by_xpath('/html/body/div[5]/div/div[2]/div[1]/main/form/div[1]/div/div[1]/input').send_keys(dicts["original_password"])
                sleep(1)
            if dicts["new_password"] is not None:
                # 输入新密码
                driver.find_element_by_xpath('/html/body/div[5]/div/div[2]/div[1]/main/form/div[2]/div/div/input').send_keys(dicts["new_password"])
                sleep(1)
            if dicts["new_password2"] is not None:
                # 确认密码
                driver.find_element_by_xpath('/html/body/div[5]/div/div[2]/div[1]/main/form/div[3]/div/div[1]/input').send_keys(dicts["new_password2"])
                sleep(1)
            # 保存
            driver.find_element_by_xpath('/html/body/div[5]/div/div[2]/div[2]/button[2]/span').click()
            sleep(2)
            if dicts["original_password"] is None:
                error_text = driver.find_element_by_xpath('/html/body/div[5]/div/div[2]/div[1]/main/form/div[1]/div/div[2]').text
                print(error_text)
                returns_list.append(error_text)
                driver.refresh()
                continue
            if dicts["new_password"] is None:
                error_text = driver.find_element_by_xpath('/html/body/div[5]/div/div[2]/div[1]/main/form/div[2]/div/div[2]').text
                error_text2 = driver.find_element_by_xpath('/html/body/div[5]/div/div[2]/div[1]/main/form/div[3]/div/div[2]').text
                print(error_text, error_text2)
                returns_list.append(error_text + error_text2)
                driver.refresh()
                continue
            if dicts["new_password2"] is None:
                error_text = driver.find_element_by_xpath('/html/body/div[5]/div/div[2]/div[1]/main/form/div[3]/div/div[2]').text
                print(error_text)
                returns_list.append(error_text)
                driver.refresh()
                continue
            if dicts["new_password"] != dicts["new_password2"]:
                error_text = driver.find_element_by_xpath('/html/body/div[5]/div/div[2]/div[1]/main/form/div[3]/div/div[2]').text
                print(error_text)
                returns_list.append(error_text)
                driver.refresh()
                continue
            if dicts["original_password"] != dicts["password"] and 8 <= len(dicts["new_password"]) <= 32 and dicts["new_password"] == dicts["new_password2"]:
                alert_text = driver.find_element_by_xpath('/html/body/div[7]/p').text
                print(alert_text)
                returns_list.append(alert_text)
                driver.refresh()
                continue
            if 8 <= len(dicts["new_password"]) <= 32 and dicts["new_password"] == dicts["new_password2"]:
                returns_list.append("密码修改成功")
            else:
                alert_text = driver.find_element_by_xpath('/html/body/div[5]/div/div[2]/div[1]/main/form/div[2]/div/div[2]').text
                print(alert_text)
                returns_list.append(alert_text)
                driver.refresh()
                continue
        print(returns_list)
        return returns_list

    def webui5(self, username):
        # 登录
        driver = login_test(username, self.driver, self.new_password)
        # 告警信息
        driver.find_element_by_xpath('//*[@id="app"]/section/header/div/aside/ul/li[2]/div').click()
        sleep(1)
        driver.find_element_by_xpath('/html/body/ul[2]/li/li').click()
        sleep(2)

        return "您访问的页面不存在 404"

    def webui6(self, username):
        # 登录
        driver = login_test(username, self.driver, self.new_password)
        # FAQ
        driver.find_element_by_xpath('//*[@id="app"]/section/header/div/aside/ul/li[3]/div').click()
        sleep(1)
        driver.find_element_by_xpath('/html/body/ul[3]/li/li').click()
        sleep(2)
        return "执行成功"

    def webui7(self, username):
        # 登录
        driver = login_test(username, self.driver, self.new_password)
        # 关于我们
        driver.find_element_by_xpath('//*[@id="app"]/section/header/div/aside/ul/li[3]/div').click()
        sleep(1)
        driver.find_element_by_xpath('/html/body/ul[3]/li[2]').click()
        sleep(2)
        # 点右上角叉×
        driver.find_element_by_xpath('/html/body/div[7]/div/div/button/i').click()
        sleep(2)

        return "执行成功"

    def webui8(self, username):
        # 登录
        driver = login_test(username, self.driver, self.new_password)
        # 退出
        driver.find_element_by_xpath('//*[@id="app"]/section/header/div/aside/ul/li[4]').click()
        sleep(2)
        # 取消
        driver.find_element_by_xpath('/html/body/div[8]/div/div[3]/button[1]').click()
        sleep(2)
        # 退出
        driver.find_element_by_xpath('//*[@id="app"]/section/header/div/aside/ul/li[4]').click()
        sleep(2)
        # 确定
        driver.find_element_by_xpath('/html/body/div[8]/div/div[3]/button[2]').click()
        sleep(2)

        return "执行成功"

    def quit(self):
        """
        浏览器退出
        :return:
        """
        self.driver.quit()
